package cn.play.cardgame.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.play.cardgame.R;
import cn.play.cardgame.constant.Constant;
import cn.play.cardgame.util.ImageUtil;

public class TopPanel extends LinearLayout {
    @InjectView(R.id.top_convert)
    ImageView mTopConvert;
    @InjectView(R.id.fourking)
    ImageView mFourKing;
    private Context mContext;
    private Handler handler;

    private static final int SIMEN_XIANHONG = 1;
    private static final int WANGZONG_XIANHONG = 2;

    private int mFourLimit;
    private int mKingLimit;

    public TopPanel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initViews();
        initHandler();
    }

    public TopPanel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopPanel(Context context) {
        this(context, null);
    }

    private void initViews() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        View view = View.inflate(mContext, R.layout.top_panel, null);
        ButterKnife.inject(this, view);
        // 暂时写在这儿
//		setFourXianhong(1000);
        this.addView(view, params);
    }

    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case SIMEN_XIANHONG:
                        setFourXianhong(Constant.FOUR_RED_LIMIT);
                        handler.sendEmptyMessageDelayed(WANGZONG_XIANHONG, 3000);
                        break;

                    case WANGZONG_XIANHONG:
                        setKingXianhongUi(Constant.KING_RED_LIMIT);
                        handler.sendEmptyMessageDelayed(SIMEN_XIANHONG, 3000);
                        break;
                }
            }
        };
        handler.sendEmptyMessageDelayed(WANGZONG_XIANHONG, 3000);
    }

    public void setFourXianhong(int fourLimit) {
        mFourLimit = fourLimit;
        mFourKing.setBackgroundResource(R.drawable.top_four);
        mTopConvert.setBackgroundDrawable(ImageUtil.getInstance().bitmapToDrawable(mContext,
                ImageUtil.getConvertMapping(), String.valueOf(fourLimit)));
    }

    private void setKingXianhongUi(int kingLimit) {
        mKingLimit = kingLimit;
        mFourKing.setBackgroundResource(R.drawable.top_king);
        mTopConvert.setBackgroundDrawable(ImageUtil.getInstance().bitmapToDrawable(mContext,
                ImageUtil.getConvertMapping(), String.valueOf(mKingLimit)));
    }

    public void setKingXianhong(int kingLimit) {
        mKingLimit = kingLimit;
    }
}
